package hongcaosp.app.android.video.record;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import hongcaosp.app.android.R;

public class RecordButton extends View {

    private final Paint mPaint;
    private int progressColor;//圆环进度颜色
    private int progressBgColor;//圆环进度背景颜色
    private int recordingBgColor;//最里面圆的背景颜色 ,录制状态
    private int unrecordBgColor;//最里面圆的背景颜色 ,非录制状态
    private float progressWidth;//圆环进度宽度

    private float innerRadio;//最里面圆的半径

    private int maxValue;
    private int value;
    private int mViewCenterX;
    private int mViewCenterY;

    private int bgColor;

    private boolean isRecording;
    private RectF mRectF; //圆环的矩形区域


    public RecordButton(Context context) {
        this(context, null);
    }

    public RecordButton(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RecordButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RecordButton);
        progressColor = a.getColor(R.styleable.RecordButton_progress_color,Color.BLUE);
        progressBgColor = a.getColor(R.styleable.RecordButton_progress_bg_color,Color.GRAY);
        recordingBgColor = a.getColor(R.styleable.RecordButton_recording_color,Color.GREEN);
        unrecordBgColor = a.getColor(R.styleable.RecordButton_unrecording_color,Color.GREEN);
        progressWidth = a.getDimension(R.styleable.RecordButton_progress_width,20);
        innerRadio = a.getDimension(R.styleable.RecordButton_min_circle_radio,300);

        maxValue = a.getInt(R.styleable.RecordButton_max_progress,100);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        //防止边缘锯齿
        mPaint.setAntiAlias(true);
        bgColor = isRecording ? recordingBgColor : unrecordBgColor;

        this.setWillNotDraw(false);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        int viewWidth = getMeasuredWidth();
        int viewHeight = getMeasuredHeight();
        mViewCenterX = viewWidth / 2;
        mViewCenterY = viewHeight / 2;

        mRectF = new RectF(mViewCenterX - innerRadio - progressWidth / 2, mViewCenterY - innerRadio - progressWidth / 2,
                mViewCenterX + innerRadio + progressWidth / 2, mViewCenterY + innerRadio + progressWidth / 2);
    }

    public void recording(boolean isRecord) {
        isRecording = isRecord;
        bgColor = isRecording ? recordingBgColor : unrecordBgColor;
        postInvalidate();
    }

    public void setMaxValue(int maxValue) {
        this.maxValue = maxValue;
    }

    public void setValue(int value) {
        this.value = value;
        postInvalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBg(canvas);
        drawProgressBg(canvas);
        drawProgress(canvas);
    }

    private void drawBg(Canvas canvas) {
        mPaint.setColor(bgColor);
        canvas.drawCircle(mViewCenterX, mViewCenterY, innerRadio, mPaint);
    }

    private void drawProgress(Canvas canvas) {
        Paint ringColorPaint = new Paint(mPaint);
        ringColorPaint.setStyle(Paint.Style.STROKE);
        ringColorPaint.setStrokeWidth(progressWidth);
        ringColorPaint.setColor(progressColor);
        //逆时针旋转90度
        canvas.rotate(-90, mViewCenterX, mViewCenterY);
        int sweepProgress = value * 360 / maxValue;
        canvas.drawArc(mRectF, 360, sweepProgress, false, ringColorPaint);
        ringColorPaint.setShader(null);
    }

    private void drawProgressBg(Canvas canvas) {
        Paint ringNormalPaint = new Paint(mPaint);
        ringNormalPaint.setStyle(Paint.Style.STROKE);
        ringNormalPaint.setStrokeWidth(progressWidth);
        ringNormalPaint.setColor(progressBgColor);//圆环默认颜色为灰色
        canvas.drawArc(mRectF, 360, 360, false, ringNormalPaint);
    }
}
